package com.note.simpleadmin.module.shop.controller;
import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.entity.TbBrand;
import com.note.simpleadmin.entity.TbClassify;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.service.BrandService;
import com.note.simpleadmin.module.shop.service.ClassifyService;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/***
 *@ClaseName:BrandController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/31 0031 20:58
 */
@Controller
@RequestMapping(value = "shop/classify")
public class ClassifyController {
    private final Logger logger = LoggerFactory.getLogger(ClassifyController.class);
    @Autowired
    private ClassifyService classifyService;
    @RequestMapping(value = "index")
    public String index() {
        return "shop/goods/classify";
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String code, Integer page) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean pageBean =classifyService.findByPage(code,page);
            msg.setData(pageBean);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "all")
    @ResponseBody
    public Msg ajax_all() {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            List<TbClassify> brandList =classifyService.findAll();
            msg.setData(brandList);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "addClassify")
    public String addBrand()
    {
        return "shop/goods/addClassify";
    }
    @RequestMapping(value = "editClassify")
    public String editBrand(String id, ModelMap map) {
        TbClassify tbClassify=classifyService.getOne(id);
        map.put("classify",tbClassify);
        return "shop/goods/editClassify";
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(TbClassify classify) {
        Msg msg=new Msg();
        try {
            classifyService.save(classify);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("添加成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_update")
    @ResponseBody
    public Msg ajax_update(TbClassify classify) {
        Msg msg=new Msg();
        try {
            classifyService.update(classify);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("更新成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_del")
    @ResponseBody
    public Msg ajax_del(String id) {
        Msg msg=new Msg();
        try {
            classifyService.delete(id);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("删除成功");
        }catch (DataNotExistException e)
        {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
            msg.setMsg(e.getMessage());
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
}
